<%@page import="seedpod.webapp.LimsSession"%>
<%@page import="seedpod.model.SeedpodDO"%>
<%@page import="seedpod.model.AVPair"%>
<%@page import="seedpod.webapp.view.seedpodwidgets.LimsWidget"%>
<%@page import="seedpod.webapp.view.seedpodwidgets.LimsWidgetFactory"%>
<%@page import="seedpod.util.Util"%>
<%@page import="seedpod.webapp.view.htmlwidget.Button"%>
<%@page import="seedpod.webapp.view.htmlwidget.HiddenInput"%>
<%@page import="seedpod.exceptions.SeedpodException"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>

<%
	LimsSession limsSession = new LimsSession( request );

	// determine whether to save data after submit or to display editor form
	String submit = request.getParameter( "submitInPlaceEdit" );
	boolean saveData = (submit != null && submit.equalsIgnoreCase( "save" ));

	// gather input parameters
	String clsName = request.getParameter( "cls" );
	String attributeName = request.getParameter( "slot" );
	String oldHTML = Util.encodeHTML( request.getParameter( "oldHTML" ).trim() );
	String parentElement = request.getParameter( "elementID" );
	String oidSt = request.getParameter( "oid" );
	Integer oid = 0;
	if ( oidSt != null ) oid = Integer.parseInt( oidSt );

	// initialize obj and the attribute-vaue-pair
	SeedpodDO obj = limsSession.getSessionCachedObject( clsName, oid );
	if ( obj == null ) {
		obj = new SeedpodDO( clsName, oid, limsSession.getDataManager() );
		obj.retrieveById();
		limsSession.cacheSeedpodObject( obj );
	}
	AVPair avPair = obj.getAttributeValue( attributeName );
	LimsWidget editWidget = LimsWidgetFactory.getEditWidget( avPair );
	editWidget.setData(avPair.getValue());
	String formID = "inPlaceEditForm_" + attributeName; 	

	// initialize widget and render
	Button cancelBtn = new Button( "cancel", "cancel" );
	cancelBtn.setJscript( "onClick=\"restore('" + parentElement + "', '" + clsName + "', '" + attributeName + "', " + oid + ", '" + oldHTML + "'); return false;\"" );
	cancelBtn.setCssClass( "commandButton" );

	Button saveBtn = new Button( "submitInPlaceEdit", "save" );
	saveBtn.setJscript( "onClick =\"submitIPE('"+editWidget.getId()+"', '"+formID + "'); return false;\"" );
	saveBtn.setCssClass( "commandButton" );

	HiddenInput hi_oid = new HiddenInput( "oid", oid.toString() );
	HiddenInput hi_cls = new HiddenInput( "cls", clsName );
	HiddenInput hi_slot = new HiddenInput( "slot", attributeName );
	HiddenInput hi_parentElement = new HiddenInput( "elementID", parentElement );

	out.println( "<div id=\"" + parentElement + "_edit\">" );
	// out.println( "<div class=\"errorMsg\">" + errorMessage + "</div>" );
	out.println( "<form action=\"#\" id=\""+formID+"\">" );
	out.println( hi_oid.render() );
	out.println( hi_cls.render() );
	out.println( hi_slot.render() );
	out.println( hi_parentElement.render() );

	out.println( "<div class=\"inPlaceEdit\" >" + editWidget.doEdit()  );
	out.println( "<br>" );
	out.println( saveBtn.render() );
	out.println( cancelBtn.render() );
	out.println( "</div>" );
	out.println( "</form>" );
	out.println( "</div>" );
	
%>

